<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Komentar extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('video_model','akun_model','komentar_model'));    
		$this->load->library(array('form_validation','session','upload','image_lib'));    
		$this->load->database();    
		$this->load->helper(array('url','form','string','date')); 
			
		
	}
	
	public function tambah_komentar()
	{	
		$this->cekLog();
		$id_user = $this->session->userdata('id_user');
		$id_video = $this->input->post('id_video');
		$timezone = 'UP5';
		$daylight_saving = TRUE;
		$time = now();
		$data_komentar = array(
			'id_user' => $id_user,
			'id_video' => $id_video,
			'isi_komentar' => $this->input->post('isi_komentar'),
			'waktu_komentar' => unix_to_human(gmt_to_local($time, $timezone, $daylight_saving))
		);
		$this->komentar_model->insert_komentar($data_komentar);
		redirect('index.php/video/lihat_video/'.$id_video);
		
	}
	
	
	public function cekLog()
	{
		if(!$this->session->userdata('isLogin')) { redirect('index.php/home');}
	}
	
	
	public function data()
	{	
		$this->cekLog();
		$id_komentar = '';
		$data['komentar'] = $this->komentar_model->get_daftar_komentar();
				
		$data['tidakaktif'] = $this->akun_model->get_banyak_akun_tidak_aktif();
		$data['video_baru'] = $this->video_model->get_banyak_video_baru();
		if($this->session->flashdata('hapus_komentar_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Komentar berhasil dihapus. </h5> </div>';
		} else if($this->session->flashdata('hapus_komentar_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Maaf komentar gagal dihapus!</h5></div>';
		} else if($this->session->flashdata('validasi_komentar_sukses')){
			$data['berhasil'] = '<div class="alert alert-success"><a class="close" data-dismiss="alert">&times</a><h5> <span class="glyphicon glyphicon-thumbs-up "></span> Validasi komentar berhasil. </h5> </div>';
		} else if($this->session->flashdata('validasi_komentar_gagal')) {
			$data['berhasil'] = '<div class="alert alert-danger"><a class="close" data-dismiss="alert">&times</a><h5><span class="glyphicon glyphicon-warning-sign "></span> Maaf validasi komentar gagal!</h5></div>';
		}  else {
			$data['berhasil'] = '';
		}
		$this->load->view('halaman_master_komentar', $data);
		
	}
	
	
	public function delete_komentar($id_komentar)
	{
		$this->cekLog();
		if ($this->komentar_model->hapus_komentar($id_komentar) == TRUE) {
			$this->session->set_flashdata('hapus_komentar_sukses', time());
			redirect('index.php/komentar/data');
		} else {
			$this->session->set_flashdata('hapus_komentar_gagal', time());
			redirect('index.php/komentar/data');
		}
	}
	
	public function delete_komentar_pribadi($id_komentar, $id_video)
	{
		$this->cekLog();
		if ($this->komentar_model->hapus_komentar($id_komentar) == TRUE) {
			$this->session->set_flashdata('hapus_komentar_sukses', time());
			redirect('index.php/video/lihat_video/'.$id_video);
		} else {
			$this->session->set_flashdata('hapus_komentar_gagal', time());
			redirect('index.php/video/lihat_video/'.$id_video);
		}
	}
	
	public function update_status()
	{
		$this->cekLog();
		$this->komentar_model->update_status();
		redirect('index.php/komentar/data');
	}
}
